function [x,y,typ]=qam_demod_int(job,arg1,arg2)
x=[];y=[];typ=[]
select job
case 'plot' then
	m = arg1.model.ipar(1)
	n = arg1.model.ipar(2)
	x_cord = arg1.model.rpar(1:m)
	y_cord = arg1.model.rpar(m+1:m+n);
  constel=xy2constel(x_cord,y_cord);
  constel = [real(constel(:)) ; imag(constel(:))]
  standard_draw(arg1)
case 'getinputs' then
  [x,y,typ]=standard_inputs(arg1)
case 'getoutputs' then
  [x,y,typ]=standard_outputs(arg1)
case 'getorigin' then
  [x,y]=standard_origin(arg1)
case 'set' then
   x=arg1;
   graphics=arg1.graphics;model=arg1.model;
   exprs=graphics.exprs;

   while %t do

      [ok,x_cord,y_cord,val,exprs]=getvalue(['Set QAM demodulator block';'set constellations and value correponds to points on constellations'],...
      ['x coordinates'; 'y coordinates';...
      'value corresponds to constellations(vector of size length(x)*length(y))'],...
      list('vec',-1,'vec',-1, 'vec', -1),exprs);

      if ~ok then 
			break
	   end
      if ok then
        x_cord = mtlb_sort(x_cord(:));
      		y_cord = mtlb_sort(y_cord(:));
	       m = length(x_cord);
	       n = length(y_cord);

  	     // to make it compatabile with MQAM modulator in modnum
  	     //reshape the val provided by user
  	     val = matrix(val, m, n);
  	     val = val';
		    val = val(:);
		    if length(val) ~= m*n then
			     message('value correspones to constellations must be of length length(x)*length(y)')
			continue
		end
	  	model.dstate = zeros(m+n+1,1);
  		model.ipar = [m; n; val]
  		model.rpar=[x_cord; y_cord]
  		
        graphics.exprs=exprs;
        x.graphics=graphics; x.model=model;
        break;
      end
   end

case 'define' then
  nu=-1
  x_cord = [-1; 1]
  y_cord = [-1; 1]
  m = length(x_cord);
  n = length(y_cord);
  val = [0, 1, 2, 3]
  val = matrix(val, m, n);
  val = val';
  val = val(:);

  model=scicos_model()
  model.sim=list('qam_demod_com',5)
  model.in=[nu;nu]
  model.out=nu;
  model.evtin=[1]
  model.evtout=[]
  model.dstate = zeros(m+n+1,1);
  model.ipar = [m; n; val]
  model.rpar=[x_cord; y_cord]
    
  model.blocktype='d'
  model.firing=[]
  model.dep_ut=[%t %f]
  gr_i=['thick=xget(''thickness'')';
        'pat=xget(''pattern'')';
        'fnt=xget(''font'')';
        'ln_st=xget(""line style"")';
        'sz1=0.86*sz(1);sz2=0.86*sz(2)';
        'xxs=orig(1)+sz(1)/2';
        'yys=orig(2)+sz(2)/2';
        'nb=size(constel(:),''*'')/2';
        'if nb<16 then';
        '  ww=0.14*sz(1)';
        '  hh=0.14*sz(2)';
        'elseif nb>=32 then';
        '  ww=0.06*sz(1)';
        '  hh=0.06*sz(2)';
        'else';
        '  ww=0.1*sz(1)';
        '  hh=0.1*sz(2)';
        'end';
        'mmax=max([constel(:);imag(constel(:))])';
        'mmin=min([constel(:);imag(constel(:))])';
        'if(mmax>1)|(mmin<-1) then';
        '  aa_s=(mmax-mmin)/2';
        'else';
        '  aa_s=1';
        'end';
        'aa_s=aa_s/0.9';
        'aa1=0';
        'aa2=64*360';
        'xset(""line style"",2)';
        'xset(""line style"",ln_st)';
        'for ii=1:nb';
        '   xxx=xxs+sz1/2*constel(ii)/aa_s-ww/2';
        '   yyy=yys+sz2/2*constel(ii+nb(1))/aa_s+hh/2';
        '   xfarc(xxx,yyy,ww,hh,aa1,aa2)';
        'end';
        'xpoly([xxs;xxs],[orig(2);orig(2)+sz(2)],""lines"")';
        'xpoly([orig(1);orig(1)+sz(2)],[yys;yys],""lines"")';
        'xset(''thickness'',thick)';
        'xset(''pattern'',pat)';
        'xset(''font'',fnt(1),fnt(2))';]
  exprs=[sci2exp(x_cord),sci2exp(y_cord),sci2exp(val)];
  x=standard_define([2 2],model,exprs,gr_i)
  x.graphics.id=["QAM Demodulator"];
end
endfunction

function constel = xy2constel(x,y)
	x = x(:);
	y = y(:);
	m = length(x);
  	n = length(y);
  	constel=kron(ones(m,1), x') + %i * kron(ones(1,n), y)
endfunction
